//
//  ILPastDaysViewController.m
//  ILife
//
//  Created by lanou on 15/10/29.
//  Copyright © 2015年 ILife. All rights reserved.
//

#import "ILPastDaysViewController.h"
#import <AFNetworking.h>
#import "ILPastDaysModel.h"
#import <UIImageView+WebCache.h>
#import "XYZPhoto.h"
#import "ILPastDaysTableViewController.h"


#define IMAGEWIDTH 200
#define IMAGEHEIGHT 250


@interface ILPastDaysViewController ()
@property(nonatomic,strong)NSMutableArray *dataArr;
@property(nonatomic,strong)NSMutableArray *photos;
@property(nonatomic,strong)NSMutableArray *photosArr;
@property(nonatomic,strong)NSMutableArray *titleArr;

@end

@implementation ILPastDaysViewController

-(NSMutableArray *)titleArr
{
    if (_titleArr==nil) {
        self.titleArr = [NSMutableArray arrayWithCapacity:0];
    }
    return _titleArr;
    
}
-(NSMutableArray *)photos
{
    if (_photos==nil) {
        self.photos = [NSMutableArray arrayWithCapacity:0];
    }
    return _photos;
}


-(NSMutableArray *)dataArr
{
    if (_dataArr==nil) {
        self.dataArr = [NSMutableArray arrayWithCapacity:0];
    }
    return _dataArr;
    
}


-(void)geteData
{
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:@"http://baobab.wandoujia.com/api/v1/categories?vc=125&u=aefb3e87a84d65c53a9cff2e3161dba6c3477f20&v=1.8.1&f=iphone" parameters:nil success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        
        for (NSDictionary *dic in responseObject)
        {
            ILPastDaysModel *model = [[ILPastDaysModel alloc]init];
                [model setValuesForKeysWithDictionary:dic];
                [self.dataArr addObject:model];
            
            UIImage *img = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:model.bgPicture]]];
            [self.photos addObject:img];
       
            
            
            
            [self.titleArr addObject:model.name];
       
        }
        self.photosArr = [NSMutableArray arrayWithCapacity:0];
        
        for (int i = 0; i < 12; i++) {
            float X = arc4random()%((int)self.view.bounds.size.width - IMAGEWIDTH);
            float Y = arc4random()%((int)self.view.bounds.size.height - IMAGEHEIGHT);
            float W = IMAGEWIDTH;
            float H = IMAGEHEIGHT;
            
            XYZPhoto *photo = [[XYZPhoto alloc]initWithFrame:CGRectMake(X, Y, W, H)];
            [photo updateImage:self.photos[i]];
            
            UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, W/2, W, H/2)];
            label.textAlignment = NSTextAlignmentCenter;
            label.text = [NSString stringWithFormat:@"#%@",_titleArr[i]];
            label.textColor = [UIColor whiteColor];
            
            [photo addSubview:label];
            
            [self.view addSubview:photo];
//添加手势
            UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(doubleTapImage:)];
//设置需要点击次数 才能触发事件
            [doubleTap setNumberOfTapsRequired:2];
            [photo addGestureRecognizer:doubleTap];
            
            UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(singleTapImage:)];
            [singleTap setNumberOfTapsRequired:1];
            [photo addGestureRecognizer:singleTap];
            
            
#warning  这行很关键，意思是只有当没有检测到doubleTapGestureRecognizer 或者 检测doubleTapGestureRecognizer失败，
            [singleTap requireGestureRecognizerToFail:doubleTap];
            
            
            float alpha = i*1.0/10 + 0.2;
            [photo setImageAlphaAndSpeedAndSize:alpha];
            
            [self.photosArr addObject:photo];
        }
        
        
    } failure:^(AFHTTPRequestOperation * _Nonnull operation, NSError * _Nonnull error) {
        NSLog(@"error----%@",error);
    }];
    
}



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"往期分类";
    [self geteData];
    

    
}
- (void)singleTapImage:(UITapGestureRecognizer *)tap
{
    XYZPhoto *photo =(XYZPhoto*) tap.view;
    [UIView animateWithDuration:1 animations:^{
        
        if (photo.state == XYZPhotoStateNormal) {
            photo.oldFrame = photo.frame;
            photo.oldAlpha = photo.alpha;
            photo.oldSpeed = photo.speed;
            photo.frame = CGRectMake(20, 20, photo.superview.bounds.size.width - 40, photo.superview.bounds.size.height - 40);
            photo.imageView.frame = photo.bounds;
            photo.drawView.frame = photo.bounds;
            [photo.superview bringSubviewToFront:photo];
            photo.speed = 0;
            photo.alpha = 1;
            photo.state = XYZPhotoStateBig;
            
            UILabel *label = photo.subviews.lastObject;
            label.center = photo.center;
            
        } else if (photo.state == XYZPhotoStateBig) {
            photo.frame = photo.oldFrame;
            photo.alpha = photo.oldAlpha;
            photo.speed = photo.oldSpeed;
            photo.imageView.frame = photo.bounds;
            photo.drawView.frame = photo.bounds;
            photo.state = XYZPhotoStateNormal;
            
            UILabel *label = photo.subviews.lastObject;
            label.center = photo.center;
            
        }
        
    }];
    
    
    
}





-(void)doubleTapImage:(UITapGestureRecognizer *)tap
{
    
    UIView *view = tap.view;
    UILabel *label = view.subviews.lastObject;

    ILPastDaysTableViewController *pastDaysTVC = [[ILPastDaysTableViewController alloc]init];
    NSString *str = [[label.text substringFromIndex:1]stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    pastDaysTVC.catagoryStr =  str;
    NSLog(@"%@",str);
    
    [self.navigationController pushViewController:pastDaysTVC animated:YES];
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
